package cn.edu.hqu.example.bj.experiment_2;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class FrameLayoutActivity extends AppCompatActivity {

    private int currentImage = 0;

    final int[] images = new int[]
            {

                    R.mipmap.first,
                    R.mipmap.second,
                    R.mipmap.third,
                    R.mipmap.four,
                    R.mipmap.five
                    //R.drawable.fifthimage
            };
    final int[] name = new int[]
            {
                    R.id.imageView1,
                    R.id.imageView2,
                    R.id.imageView3,
                    R.id.imageView16,
                    R.id.imageView5
            };
    ImageView[] views = new ImageView[name.length];

    @SuppressLint("HandlerLeak")
    Handler handler =new Handler()
    {
        @Override
        public  void handleMessage(Message msg)
        {
            //means the message comes from this program
            if(msg.what==0x123)
            {
                for(int i=0;i< name.length;i++)
                {
                    views[i].setImageResource(images[(i+currentImage)%name.length]);
                }
                currentImage++;
            }

        }

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_layout);

        Button bt_15=(Button)findViewById(R.id.bt_15);
        bt_15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(FrameLayoutActivity.this,MainActivity.class);
                startActivity(i);
            }
        });

        for(int i=0;i<name.length;i++)
        {
            views[i]=(ImageView)findViewById(name[i]);
        }
        //Define a thread to change the picture periodically
        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                //send an empty message to info system change the picture of the six ImageVie
                handler.sendEmptyMessage(0x123);
            }
        },0,2000);



    }


}
